package com.gba.mark.exam.service.Impl;

import com.alibaba.fastjson.JSONObject;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gba.mark.exam.entity.ExamPaper;
import com.gba.mark.exam.entity.ExamQuestions;
import com.gba.mark.exam.mapper.ExamPaperMapper;
import com.gba.mark.exam.service.ExamPaperService;
import com.gba.mark.exam.service.ExamQuestionsService;
import com.gba.mark.group.entity.Group;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Service
public class ExamPaperImpl extends ServiceImpl<ExamPaperMapper, ExamPaper> implements ExamPaperService {

    @Resource
    private ExamPaperMapper examPaperMapper;

    @Autowired
    private ExamPaperService examPaperService;

    @Autowired
    private ExamQuestionsService examQuestionsService;

    /**
     *保存并更新一份试卷
     */
    @Override
    public int savePaper(List<ExamQuestions> questionsList, int group_id) {
        JSONObject questionIds = new JSONObject();
        int index = 1;
        for (ExamQuestions question : questionsList) {
            examQuestionsService.saveQuestion(question);
            int question_id = question.getId();
            questionIds.put(String.valueOf(index++), question_id);
        }

        ExamPaper examPaper = new ExamPaper();
        examPaper.setQuestionIds(questionIds.toString());
        examPaper.setGroupId(group_id);
        return this.baseMapper.insert(examPaper);
    }

    /**
     *删除一份试卷
     */
    @Override
    public boolean removeExamPaper(Integer group_id) {
        //删除对应的试卷类型数据

        return examPaperService.remove(new QueryWrapper<ExamPaper>().eq("group_id",group_id));
    }

    /**
     * 给新入组的用户展示试卷(查询)
     */
    @Override
    public List<ExamPaper> findPaper(int group_id){
        return examPaperMapper.findPaper(group_id);
    }

}

